import requests
from AIAnalyzer import DeepSeekCodeAnalyzer
from deal_result import load_json_result, write_result_excel_v2
from markdownify import markdownify as md

if __name__ == '__main__':
    ds = DeepSeekCodeAnalyzer()
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
    }
    get_document_url = 'https://svc-drcn.developer.huawei.com/community/servlet/consumer/cn/documentPortal/getDocumentById'

    urls = ['https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/application-dev-guide',
            'https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/concurrency-faq']
    for url in urls:
        catalog_name = url.split('/doc/')[1].split('/')[0]
        object_id = url.split('/')[-1]
        if object_id == '':
            object_id = url.split('/')[-2]
        data = {
            "objectId": object_id,
            "version": "",
            "catalogName": catalog_name,
            "language": "cn"
        }

        res = requests.post(url=get_document_url, headers=headers, data=data)
        content = res.json()['value']['content']['content']
        title = res.json()['value']['title']
        md_content = md(content)
        print(md_content)
        ds.analyze_md_content(md_content, url, catalog_name, title)
        ds.message = []
    analyzer_results = load_json_result('.code_cache')
    write_result_excel_v2(analyzer_results)
